
import os
from cut import auto_cut
import win_unicode_console

win_unicode_console.enable()
import sys

from PyQt5.QtWidgets import (QWidget, QMessageBox, QPushButton, QLineEdit, QLabel, QApplication, QFileDialog, QCheckBox,
                             QRadioButton)

class autoCut(QWidget):
    row_number = 0

    def __init__(self):
        super(autoCut, self).__init__()
        self.initUI()

    def initUI(self):
        self.rember_dest = 'D:/'  # 记住目标文件夹
        self.rember_source = 'D:/'  # 记住源文件夹
        self.files = []

        # 源文件选择按钮和选择编辑框
        self.source_btn, self.source_le = self.create_btn_and_editline('源文件', self.select_source)

        # 目标文件夹选择按钮和选择编辑框
        self.target_btn, self.target_le = self.create_btn_and_editline('目标路径', self.select_target)

        # 最小停顿时间 输入框和提示
        self.min_time_le = self.create_label_input("最小停顿时间", '0.1')
        # 停顿最小音量 输入框和提示
        self.min_volume_le = self.create_label_input("停顿最小音量", '2000')
        self.create_btn('说话停顿剪辑', self.autoCut)

        # 整体界面设置
        self.setGeometry(320, 320, 400, 250)
        self.setWindowTitle('MagicCut')  # 设置界面标题名
        self.show()

    # 创建标签和输入框，在一行
    def create_label_input(self, text, value=''):
        self.row_number += 1
        label = QLabel(self)
        label.move(30, 32 * self.row_number)
        label.resize(90, 30)
        label.setText(text)
        le = QLineEdit(self)
        le.move(120, 32 * self.row_number)
        le.resize(250, 30)
        le.setText(value)
        return le

    # 创建一个按钮
    def create_btn(self, btn_text, btn_function):
        self.row_number += 1
        btn = QPushButton(btn_text, self)
        btn.move(30, 32 * self.row_number)  # 位置 (x,y)
        btn.resize(120, 30)  # 尺寸 (长,宽)
        btn.clicked.connect(btn_function)

    # 创建一个单选按钮
    def create_radio(self, text):
        self.row_number += 1
        btn = QRadioButton(text, self)
        btn.move(30, 32 * self.row_number)  # 位置 (x,y)
        return btn

    # 创建并排两个复选框
    def create_checkbox(self, text1, text2):
        self.row_number += 1
        cb1 = QCheckBox(text1, self)
        cb1.move(30, 32 * self.row_number)  # 位置 (x,y)
        cb1.toggle()

        cb2 = QCheckBox(text2, self)
        cb2.move(90, 32 * self.row_number)  # 位置 (x,y)
        cb2.toggle()
        return cb1, cb2

    # 创建按钮和输入框在一行
    def create_btn_and_editline(self, btn_text, btn_function):
        self.row_number += 1
        btn = QPushButton(btn_text, self)
        btn.move(30, 32 * self.row_number)  # 位置 (x,y)
        btn.resize(60, 30)  # 尺寸 (长,宽)
        btn.clicked.connect(btn_function)
        le = QLineEdit(self)
        le.move(120, 32 * self.row_number)
        le.resize(250, 30)
        return btn, le

    # 弹出消息提示框
    def notice(self, message):
        # QSystemTrayIcon.showMessage(message)
        QMessageBox.information(self, '提示', message, QMessageBox.Close)

    # 打开的视频文件名称
    def select_source(self):
        self.files = []
        files, ok1 = QFileDialog.getOpenFileNames(self, "选择源文件", self.rember_source)
        t = ";".join(files)
        parent_dir = os.path.dirname(files[0]);
        self.rember_source = parent_dir  # 记住上次打开的文件夹，第一个文件的父文件夹
        self.source_le.setText(t)
        self.files = files

    # 选择保存路径
    def select_target(self):
        dir = QFileDialog.getExistingDirectory(self, "选择保存路径", self.rember_dest)
        self.rember_dest = dir
        self.target_le.setText(dir)

    '''说话停顿剪辑'''
    def autoCut(self):
        target = self.target_le.text().strip()  # 获取剪切后视频保存的目录
        min_time = float(self.min_time_le.text().strip())
        min_volume = int(self.min_volume_le.text().strip())

        for f in self.files:
            auto_cut(f, target, min_time, min_volume)

        self.notice('说话停顿剪辑成功!')

if __name__ == "__main__":
    # pyinstaller.exe –p moviepy -F 'D:/video/AutoCut.py'
    app = QApplication(sys.argv)
    ex = autoCut()
    sys.exit(app.exec_())
